<?php
// include function files for this application
require_once('/home/domains/uny-con.com/public_html/uny-con_fns.php');
if (!session_id()) { session_start(); }

   do_html_header("UNY-Con Create text file of events", staysmall, $uny);

    //connect to the DB
   if (!($conn = db_connect()))
     return false;

$prev_event_array[] = "";

$basedir = 'files/';
function check_write_file($filename) {
//fopen ($filename, 'w+');
if (is_writable($filename)) {
	// open file as writeable and zero it.
	if (!$fp = fopen($filename, 'w+b')) {
		print "<span class=\"redtext\">Cannot open file ($filename)</span>";
		exit;
		}
	//print "Success, wrote ($someconent entries) to file ($filename)";
	fclose($fp);
	} # close iswriteable if
	else {
	echo "<span class=\"redtext\">File is not writeable!</span>";
	}
}


$result = get_event_list();
$num = pg_numrows($result);
$i=0;
while ($i < $num) {
	$values = recordproc($result, $i);
	extract($values);
	//This checks to see what timeslot the current record is in. If we have moved to a new timeslot from the previous record we clos the previous table and start a new one, otherwise we write another table cell.
	$current_ts = $starting_timeslot;
	$current_event_type = $event_type;
	if ($working_ts != $current_ts) {
		$working_ts = $current_ts;
		$tstime = print_timeslot($current_ts);
		echo "$tstime<br />\n";
/*
		$filename = "TS"."$working_ts"." $event_type".".csv";
		check_write_file("$basedir"."$filename");
		$somecontent = "\n\n$tstime";
		write_to_file($somecontent, "$basedir"."$filename");
		do_html_URL("$basedir"."$filename", "Download $filename");
		print_brs(1);
*/
		}
	if ($working_event_type != $current_event_type) {
		$working_event_type = $current_event_type;
		$filename = "TS"."$working_ts"." $event_type".".csv";
		check_write_file("$basedir"."$filename");
		$somecontent = "\"Event Title\",\"System\",\"GM\",\"Event Type\",\"Event Code\"";
		write_to_file($somecontent, "$basedir"."$filename");
		do_html_URL("$basedir"."$filename", "Download $filename");
		echo "<br />\n";
		}
		unset($processed_numts, $processed_equip, $processed_event_id, $processed_maturity, $print_description);
		//this gets rid of mulitple \n characters.
		$print_description = str_replace("\n\n" , "", $description);
		$print_description = str_replace("," , ";comma;", $print_description);
		$event_title = str_replace("," , ";comma;", $event_title);
		$game_system = str_replace("," , ";comma;", $game_system);
		if ($num_of_timeslots != "1") {$processed_event_id = "TS$starting_timeslot"."$event_id*"; }
			else {$processed_event_id = "TS$starting_timeslot"."$event_id"; }
		if ($num_of_timeslots != "1") {$processed_numts = "\n*Note;comma; this event lasts $num_of_timeslots timeslots"; }
		if ($equipment != "") {$processed_equip = "\n\"Attendees should bring: $equipment\""; }
		$processed_equip = str_replace("," , ";comma;", $processed_equip);
		if ($maturity == "M") {$processed_maturity = "\n\"This event may contain mature themes\""; }
		if ($maturity == "Y") {$processed_maturity = "\n\"This event will contain mature themes\""; }
		if ($cost > 0) {$processed_cost = "\n\"This event has an additional fee of "."$"."$cost\""; }
		if (array_key_exists($event_id, $prev_event_array)) {
			$print_description = "Please see event description for event TS".$prev_event_array["$event_id"]."$event_id in timeslot ".$prev_event_array["$event_id"].".";
			unset($processed_equip, $processed_maturity);
			}
		$somecontent = "\n\"$event_title\",\"$game_system\",\"$name\",\"$event_type\",\"$processed_event_id\"
\"$print_description\""."$processed_maturity"."$processed_equip"."$processed_numts"."$processed_cost";
		write_to_file($somecontent, "$basedir"."$filename");
		$processed_game_system = str_replace("Dungeons & Dragons","D&D","$game_system");
		if (!array_key_exists($event_id, $prev_event_array)) $prev_event_array["$event_id"] = "$starting_timeslot";
		unset($somecontent, $quickcontent, $cost, $processed_maturity, $processed_equip, $processed_numts, $processed_cost, $maturity, $equipment);
	$i++;
	}


do_html_footer(news);